﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(TextI18N))]
    public class TextI18NEditor : Editor
    {
        private SerializedProperty font;
        protected SerializedProperty keyid;
        protected SerializedProperty format, Placeholder;
        
        protected SerializedProperty binds;
        protected SerializedProperty bindnewlines;
        protected SerializedProperty bindBestFits;

        protected SerializedProperty onLanguageChange;
        protected ReorderableList reorderableList,reorderableList2, reorderableList3;

        protected SerializedProperty TranslateLanguage;

        private static bool IsExpConfigNewLine,IsExpFixed,IsExpLanguageChange,IsBestFit;
            
        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            if (this.font != null)
            {
                EditorGUILayout.Space(5f);
                EditorGUILayout.PropertyField(this.font, new GUILayoutOption[0]);
                if(this.font.objectReferenceValue == null) this.font.objectReferenceValue = FindI18NFont();
                EditorGUILayout.Space(1f);
                EditorGUILayout.PropertyField(this.keyid, new GUILayoutOption[0]);
                if (!string.IsNullOrEmpty(keyid.stringValue))
                {
                    EditorGUILayout.Space(1f);
                    EditorGUILayout.PropertyField(this.format, new GUILayoutOption[0]);
                    EditorGUILayout.Space(1f);
                    EditorGUILayout.PropertyField(this.Placeholder, new GUILayoutOption[0]);   
                }
                EditorGUILayout.Space(5f);
            }
            DrawTextBestFit();
            EditorGUILayout.Space(10f);
            DrawTextNewLine();
            EditorGUILayout.Space(10f);
            DrawFixedText();

            EditorGUILayout.Space(10f);
            DrawTextChangeEvent();
            EditorGUILayout.Space(10f);

            serializedObject.ApplyModifiedProperties();

            EditorGUILayout.PropertyField(this.TranslateLanguage, new GUILayoutOption[0]);
            EditorGUILayout.BeginHorizontal();
            if (GUILayoutEx.ActionButton("Chinese", new GUILayoutOption[0]))
            {
                base.serializedObject.Update();
                Translatable translate = (Translatable)this.target;
                translate.TestSwitchLanugage(1);
                base.serializedObject.ApplyModifiedProperties();
            }
            if (GUILayoutEx.ActionButton("Translate", new GUILayoutOption[0]))
            {
                base.serializedObject.Update();
                Translatable translate = (Translatable)this.target;
                translate.TestSwitchLanugage(this.TranslateLanguage.intValue);
                base.serializedObject.ApplyModifiedProperties();
            }
      
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space(5f);
            serializedObject.ApplyModifiedProperties();
        }

        protected virtual void OnEnable()
        {
            if (this.target == null) return;
            
            this.binds = serializedObject.FindProperty("binds");
            this.bindnewlines = serializedObject.FindProperty("bindNewlines");
            this.bindBestFits = serializedObject.FindProperty("bindBestFits");

            this.keyid = serializedObject.FindProperty("keyid");
            this.format = serializedObject.FindProperty("format");
            this.Placeholder = serializedObject.FindProperty("Placeholder");
            this.TranslateLanguage = serializedObject.FindProperty("TranslateLanguage");

            this.font = serializedObject.FindProperty("i18nfont");
            this.onLanguageChange = serializedObject.FindProperty("onLanguageChange");

            this.reorderableList = new ReorderableList(serializedObject, this.binds);
            this.reorderableList.drawHeaderCallback = OnDrawHead;
            this.reorderableList.elementHeight = EditorGUIUtility.singleLineHeight;
            this.reorderableList.drawElementCallback = OnDrawElement;
            this.reorderableList.onAddCallback = OnAddElement;

            this.reorderableList2 = new ReorderableList(serializedObject, this.bindnewlines);
            this.reorderableList2.drawHeaderCallback = OnDrawHead2;
            this.reorderableList2.elementHeight = EditorGUIUtility.singleLineHeight;
            this.reorderableList2.drawElementCallback = OnDrawElement2;
            this.reorderableList2.onAddCallback = OnAddElement2;

            this.reorderableList3 = new ReorderableList(serializedObject, this.bindBestFits);
            this.reorderableList3.drawHeaderCallback = OnDrawHead3;
            this.reorderableList3.elementHeight = EditorGUIUtility.singleLineHeight;
            this.reorderableList3.drawElementCallback = OnDrawElement3;
            this.reorderableList3.onAddCallback = OnAddElement3;
            
        }

        private void DrawTextBestFit()
        {
            IsBestFit = GUILayoutEx.Title("文本缩放", IsBestFit);
            if (!IsBestFit) return;
            EditorGUILayout.Space(5f);
            reorderableList3.DoLayoutList();
        }
        private void DrawTextNewLine()
        {
            IsExpConfigNewLine = GUILayoutEx.Title("文本换行", IsExpConfigNewLine);
            if (!IsExpConfigNewLine) return;
            EditorGUILayout.Space(5f);
            reorderableList2.DoLayoutList();
        }

        private void DrawFixedText()
        {
            IsExpFixed = GUILayoutEx.Title("文本固定", IsExpFixed);
            if (!IsExpFixed) return;
            EditorGUILayout.Space(5f);
            reorderableList.DoLayoutList();
        }


        private void DrawTextChangeEvent()
        {
            IsExpLanguageChange = GUILayoutEx.Title("文本动态修改事件", IsExpLanguageChange);
            if (!IsExpLanguageChange) return;
            EditorGUILayout.Space(5f);
            EditorGUILayout.HelpBox("语言切换时，代码控制的文本无法实时切换语言，需要添加回调事件.", MessageType.Info);
            EditorGUILayout.PropertyField(this.onLanguageChange);

        }

        protected void OnDrawHead(Rect rect)
        {
            Rect position = new Rect(rect.x + 15f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position2 = new Rect(rect.x + 15f + rect.width / 2f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            GUI.Label(position, "Language");
            GUI.Label(position2, "Translate");
        }
        protected void OnDrawHead2(Rect rect)
        {
            Rect position = new Rect(rect.x + 15f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position2 = new Rect(rect.x + 15f + rect.width / 4f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position3 = new Rect(rect.x + 15f + (rect.width / 4f)*2, rect.y,  rect.width - (rect.width / 4f) *2, EditorGUIUtility.singleLineHeight);
            GUI.Label(position, "Language");
            GUI.Label(position2, "NewLine");
            GUI.Label(position3, "New Line Length");
        }

        protected void OnDrawHead3(Rect rect)
        {
            Rect position = new Rect(rect.x + 15f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect position2 = new Rect(rect.x + 15f + rect.width / 2f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            GUI.Label(position, "Language");
            GUI.Label(position2, "BestFit");
        }

        protected void OnDrawElement(Rect rect, int index, bool isActive, bool isForce)
        {
            SerializedProperty arrayElementAtIndex = this.binds.GetArrayElementAtIndex(index);

            SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
            SerializedProperty translate = arrayElementAtIndex.FindPropertyRelative("translate");

            Rect recttime = new Rect(rect.x, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect recttime2 = new Rect(rect.x + rect.width / 4f+6f, rect.y, rect.width - rect.width / 4f, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(recttime, languagetype, GUIContent.none);
            EditorGUI.PropertyField(recttime2, translate, GUIContent.none);
        }
        protected void OnDrawElement2(Rect rect, int index, bool isActive, bool isForce)
        {
            SerializedProperty arrayElementAtIndex = this.bindnewlines.GetArrayElementAtIndex(index);

            SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
            SerializedProperty isNewline = arrayElementAtIndex.FindPropertyRelative("isNewline");
            SerializedProperty length = arrayElementAtIndex.FindPropertyRelative("length");

            Rect recttime = new Rect(rect.x, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect recttime2 = new Rect(rect.x + rect.width / 4f + 6f, rect.y,  rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect recttime3 = new Rect(rect.x + (rect.width / 4f + 6f)*2, rect.y, rect.width - (rect.width / 4f)*2, EditorGUIUtility.singleLineHeight);

            EditorGUI.PropertyField(recttime, languagetype, GUIContent.none);
            EditorGUI.PropertyField(recttime2, isNewline, GUIContent.none);
            EditorGUI.PropertyField(recttime3, length, GUIContent.none);
        }

        protected void OnDrawElement3(Rect rect, int index, bool isActive, bool isForce)
        {
            SerializedProperty arrayElementAtIndex = this.bindBestFits.GetArrayElementAtIndex(index);

            SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
            SerializedProperty isBestFit = arrayElementAtIndex.FindPropertyRelative("isBestFit");

            Rect recttime = new Rect(rect.x, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
           // Rect recttime2 = new Rect(rect.x + rect.width / 4f + 6f, rect.y, rect.width - rect.width / 4f, EditorGUIUtility.singleLineHeight);
            Rect recttime2 = new Rect(rect.x + 15f + rect.width / 2f, rect.y, rect.width / 4f, EditorGUIUtility.singleLineHeight);
            EditorGUI.PropertyField(recttime, languagetype, GUIContent.none);
            EditorGUI.PropertyField(recttime2, isBestFit, GUIContent.none);
        }

        protected void OnAddElement(ReorderableList list)
        {
            if (list.serializedProperty != null)
            {
                list.serializedProperty.arraySize++;
                list.index = list.serializedProperty.arraySize - 1;

                SerializedProperty arrayElementAtIndex = list.serializedProperty.GetArrayElementAtIndex(list.index);
                SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
                SerializedProperty translate = arrayElementAtIndex.FindPropertyRelative("translate");
                languagetype.enumValueIndex = 0;
                translate.stringValue = "";
            }
            else
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
            }
        }
        protected void OnAddElement2(ReorderableList list)
        {
            if (list.serializedProperty != null)
            {
                list.serializedProperty.arraySize++;
                list.index = list.serializedProperty.arraySize - 1;

                SerializedProperty arrayElementAtIndex = list.serializedProperty.GetArrayElementAtIndex(list.index);
                SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
                SerializedProperty isNewline = arrayElementAtIndex.FindPropertyRelative("isNewline");
                SerializedProperty length = arrayElementAtIndex.FindPropertyRelative("length");
                languagetype.enumValueIndex = 0;
                isNewline.boolValue = false;
                length.intValue = 0;
            }
            else
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
            }
        }

        protected void OnAddElement3(ReorderableList list)
        {
            if (list.serializedProperty != null)
            {
                list.serializedProperty.arraySize++;
                list.index = list.serializedProperty.arraySize - 1;

                SerializedProperty arrayElementAtIndex = list.serializedProperty.GetArrayElementAtIndex(list.index);
                SerializedProperty languagetype = arrayElementAtIndex.FindPropertyRelative("languagetype");
                SerializedProperty isBestFit = arrayElementAtIndex.FindPropertyRelative("isBestFit");
                languagetype.enumValueIndex = 0;
                isBestFit.boolValue = false;
            }
            else
            {
                ReorderableList.defaultBehaviours.DoAddButton(list);
            }
        }


        private I18NFont FindI18NFont()
        {
            string[] array = AssetDatabase.FindAssets("t:I18NFont");
            if (array.Length == 0)
            {
                return null;
            }
            string text = AssetDatabase.GUIDToAssetPath(array[0]);
            return AssetDatabase.LoadAssetAtPath<I18NFont>(text);
        }

    }
}

